package library.and.xps.com.step.view.activity;

import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jude.utils.JUtils;
import com.orhanobut.dialogplus.DialogPlus;
import com.orhanobut.dialogplus.ListHolder;
import com.orhanobut.dialogplus.OnItemClickListener;
import com.orhanobut.dialogplus.ViewHolder;
import com.squareup.okhttp.Request;

import java.util.List;

import library.and.xps.com.step.R;
import library.and.xps.com.step.URLConstants;
import library.and.xps.com.step.adapter.DepartmentListAdapter;
import library.and.xps.com.step.adapter.MemberListAdapter;
import library.and.xps.com.step.adapter.MemberListAdapterInDepartDetailAdapter;
import library.and.xps.com.step.m.model.MemberInDepartment;
import library.and.xps.com.step.m.model.TeamManager;
import library.and.xps.com.step.m.netapi.OkHttpClientUtils;
import library.and.xps.com.step.net.ResponceWrapper;
import library.and.xps.com.step.util.CommonUtils;
import library.and.xps.com.step.util.HttpResponsHelper;
import library.and.xps.com.step.util.RequestCallback;
import library.and.xps.com.step.util.TeamHelper;
import library.and.xps.com.step.view.base.BaseActivity;

public class ChooseMemberChallengerToActivity extends BaseActivity {
    ListView lv_member;
    ListView lv_department;

    MemberListAdapter memberListAdapter;
    DepartmentListAdapter departmentListAdapter;

    TeamManager teamManager;

    @Override
    protected int getLayout() {
        return R.layout.activity_member_challengerto;
    }

    @Override
    protected void setData() {
        memberListAdapter = new MemberListAdapter(this);
        departmentListAdapter = new DepartmentListAdapter(this);
        lv_department.setAdapter(departmentListAdapter);
        lv_member.setAdapter(memberListAdapter);

        //todo 暂时先不写
        lv_member.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {//默认部门成员列表点击

            }
        });

        lv_department.setOnItemClickListener(new AdapterView.OnItemClickListener() {//部门列表点击
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                List<TeamManager.ListBean> data = ((DepartmentListAdapter) parent.getAdapter()).getData();
                getUsersInTeam(data.get(position).getDepartmentid());
            }
        });

        getUserDepartments();

    }

    //根据DId 获取其中成员
    void getUsersInTeam(int departmentId) {
        if (getUser() == null) return;
        String data = URLConstants.TEAM.ManageDepartmentUsers + "?userid=" + getUser().getUserid() + "&departmentid=" + departmentId;
        OkHttpClientUtils.getDataAsync(this, data, new RequestCallback() {
            @Override
            public void onFailure(Request request, Exception e) {
                dissmisDialog();
                e.printStackTrace();
            }

            @Override
            public void onResponse(ResponceWrapper response) {
                dissmisDialog();

                if (HttpResponsHelper.SuccessResponce(response)) {
                    MemberInDepartment memberInDepartment = new Gson().fromJson(response.getBody(), MemberInDepartment.class);
                    if (memberInDepartment.getList().size() <= 0) {//部门成员不足一人的时候 返回
                        JUtils.Toast("团队中没有成员");
                        return;
                    }

                    MemberListAdapterInDepartDetailAdapter m = new MemberListAdapterInDepartDetailAdapter(ChooseMemberChallengerToActivity.this);
                    m.setData(memberInDepartment.getList());
                    showdialog("请选择成员:", m);
                }
            }

            @Override
            public void onStart() {
                showDialogNoCancel();
            }
        }, "");

    }

    @Override
    protected void init() {
        lv_department = r2v(R.id.lv_department);
        lv_member = r2v(R.id.lv_member);
        ((TextView) r2v(R.id.actionbar_tv_title)).setText("选择PK对象");
        r2v(R.id.actionbar_iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    //显示部门中成员的dialog
    void showdialog(String hintText, BaseAdapter adapter) {
        ViewHolder headerViewHolder = new ViewHolder(getLayoutInflater().inflate(R.layout.header, null));
        View headerView = headerViewHolder.getInflatedView();
        ((TextView) headerView.findViewById(R.id.tv_hint)).setText(hintText);

        DialogPlus dialog = DialogPlus.newDialog(this)
                .setContentHolder(new ListHolder())
                .setGravity(Gravity.CENTER)
                .setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
                        MemberInDepartment.ListBean member = ((MemberInDepartment.ListBean) item);
                        if(member.getUserid()==Integer.valueOf(getUser().getUserid())){
                            JUtils.Toast("不能选择自己!");
                            return;
                        }

                        dialog.dismiss();
                        Intent memberI = new Intent();
                        memberI.putExtra("member", member);
                        setResult(2, memberI);
                        finish();
                    }
                })
                .setInAnimation(R.anim.dialog_in_anim)
                .setOutAnimation(R.anim.dialog_out_anim)
                .setAdapter(adapter)
                .setHeader(headerView)
                .setExpanded(true, 400)
                .create();
        dialog.show();

    }

    /**
     * 获取部门列表
     */
    void getUserDepartments() {
        if (getUser() == null) return;
        String data = URLConstants.TEAM.ManageDepartment + "?userid=" + getUser().getUserid() + "&teamid=" + TeamHelper.getIns().getCurT().getTeamid();
        OkHttpClientUtils.getDataAsync(this, data, new RequestCallback() {
            @Override
            public void onFailure(Request request, Exception e) {
                dissmisDialog();
                e.printStackTrace();
            }

            @Override
            public void onResponse(ResponceWrapper response) {
                dissmisDialog();

                if (HttpResponsHelper.SuccessResponce(response)) {
                    teamManager = new Gson().fromJson(response.getBody(), TeamManager.class);
                    List<TeamManager.ListBean> list = teamManager.getList();
                    departmentListAdapter.setData(list);
                    CommonUtils.resetListViewHeight(lv_department);

                }
            }

            @Override
            public void onStart() {
                showDialogNoCancel();
            }
        }, "");
    }

}
